#region using statements

using System.Collections.Generic;

using Microsoft.Xna.Framework;

#endregion

namespace NGen.Systems.StringAlarms
{
	/// <summary>
	/// 	Class to manage the alarms
	/// </summary>
	public sealed class AlarmSystem
	{
		#region local fields

		private readonly List<AlarmObject> m_alarmsExpired;

		/// <summary>
		/// 	The list of ACTIVE alarms
		/// </summary>
		private readonly List<AlarmObject> m_alarmsList;

		private readonly List<AlarmObject> m_alarmsNew;

		#endregion

		#region constructors

		/// <summary>
		/// 	Constructor sets the parent reference and initializes the alarms list
		/// </summary>
		public AlarmSystem() {
			m_alarmsList = new List<AlarmObject>();
			m_alarmsExpired = new List<AlarmObject>();
			m_alarmsNew = new List<AlarmObject>();
		}

		#endregion

		#region local methods

		/// <summary>
		/// 	Add an alarm to the system.
		/// </summary>
		/// <param name = "p_alarm">the alarm to add</param>
		public void Add(AlarmObject p_alarm) {
			m_alarmsNew.Add(p_alarm);
		}

		/// <summary>
		/// 	Update all of the alarms in the list
		/// </summary>
		/// <param name = "p_gameTime">the updated game time</param>
		public void Update(GameTime p_gameTime) {
			//update the alarms
			foreach(var alarm in m_alarmsList) {
				//if the alarm update returns true then it has expired and remove from the list
				if(alarm.Update(p_gameTime)) {
					m_alarmsExpired.Add(alarm);
				}
			}

			//remove all expired alarms
			foreach(var alarm in m_alarmsExpired) {
				m_alarmsList.Remove(alarm);
			}

			//add all new alarms to the list to update
			foreach(var alarm in m_alarmsNew) {
				m_alarmsList.Add(alarm);
			}

			m_alarmsNew.Clear();
			m_alarmsExpired.Clear();
		}

		/// <summary>
		/// 	Removes all alarms from the alarm system
		/// </summary>
		public void Cleanup() {
			m_alarmsList.Clear();
		}

		#endregion
	}
}